<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\Table\UserSelectTable;
use App\Admin\Repositories\Worker;
use App\Admin\Tables\UserTable;
use App\Models\Area;
use App\Models\ShopUser;
use App\Models\User;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Validation\Rule;

class WorkerController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Worker(['user','shop']), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('shop_id');
            $grid->column('shop.name','所属门店');
            $grid->column('user_id');
            $grid->column('user.nickname','用户昵称');
            $grid->column('image')->image(null,40,40);
            $grid->column('name');
            $grid->column('phone');
            $grid->column('money');
            $grid->column('used_money');
            $grid->column('area','所在区域');
            $grid->column('status')->using(['正常','隐藏'])->label(['info','default']);
            $grid->column('created_at');
//            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->expand(false);
                $filter->equal('id')->width(3);
                $filter->like('shop.name','所属门店')->width(3);
                $filter->like('user.nickname','用户昵称')->width(3);
                $filter->like('name')->width(3);
                $filter->like('phone','手机号')->width(3);
                $filter->like('area','所在区域')->width(3);
                $filter->equal("status","状态")->select(['正常','隐藏'])->width(3);
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Worker(), function (Show $show) {
            $show->field('id');
            $show->field('shop_id');
            $show->field('user_id');
            $show->field('image');
            $show->field('name');
            $show->field('phone');
            $show->field('money');
            $show->field('used_money');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Worker(), function (Form $form) {
//            $form->display('id');
            $shops = ShopUser::getOptions();
            $form->select('shop_id','所属门店')->options($shops);
            $form->selectTable('user_id','所属用户')->title('选择用户')
                ->dialogWidth('60%') // 弹窗宽度，默认 800px
                ->from(UserSelectTable::make(['id' => $form->getKey(),'is_limit'=>true, 'table' =>'worker'])) // 设置渲染类实例，并传递自定义参数
                ->model(User::class, 'id', 'nickname')->required(); // 设置编辑数据显示
            $form->image('image')->autoUpload()->help("图片尺寸要求:100*123")->required();
            $id = $form->getKey();
            $message = ['unique' => '此门店员工已存在' ];
            $rules = [
                'required'
            ];
            if($id){
                $rules[] = Rule::unique('worker','name')->where(function ($query){
                    $query->where("shop_id",request()->input("shop_id"));
                })->ignore($id);
            }else{
                $rules[] = Rule::unique('worker','name')->where(function ($query){
                    $query->where("shop_id",request()->input("shop_id"));
                });
            }
            $form->text('name')->rules($rules,$message)->required();
            $form->text('phone')->required();
            if ($form->isEditing()){
                 $form->text('money');
                $form->text('used_money');
            }
            $form->hidden("district_id");
            $form->category("district_id","所在区域")->options(Area::getOptions());
            $form->radio('status')->options(['正常','隐藏'])->default(0);
        });
    }
}
